package com.csxy.nyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.csxy.nyf.entity.EduTeacher;
import com.csxy.nyf.entity.vo.TeacherQuery;
import com.csxy.nyf.mapper.EduTeacherMapper;
import com.csxy.nyf.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Autowired
    private EduTeacherMapper eduTeacherMapper;
    @Override
    public Page<EduTeacher> pageListTeacher(long page, long limit){
        Page<EduTeacher> mypage = new Page<>(page,limit);
        Page<EduTeacher>eduTeacherPage = eduTeacherMapper.selectPage(mypage,null);
        return eduTeacherPage;
    }

    @Override
    public void PageQuery(Page<EduTeacher> pageParm, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if(teacherQuery != null){
            eduTeacherMapper.selectPage(pageParm,null);
        }
        if(teacherQuery.getName() != null && !"".equals(teacherQuery.getName())){
            queryWrapper.like("name",teacherQuery.getName());
        }
        if(teacherQuery.getLevel() != null){
            queryWrapper.eq("level",teacherQuery.getLevel());
        }
        if(teacherQuery.getBegin() != null && !"".equals(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if(teacherQuery.getEnd() != null && !"".equals(teacherQuery.getEnd())){
            queryWrapper.le("gmt_modified",teacherQuery.getEnd());
        }
        eduTeacherMapper.selectPage(pageParm,queryWrapper);

    }

    @Override
    @Cacheable(value = "teacher",key = "'selectHotTeachers'")
    public List<EduTeacher> selectHotTeachers() {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        queryWrapper.last("limit 4");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageTeacher) {
        //前台页面系统分页查询讲师的方法
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        //把分页数据封装到pageTeacher对象中
        baseMapper.selectPage(pageTeacher,queryWrapper);
        //把分页的数据获取出来返回一个map集合
        HashMap<String, Object> map = new HashMap<>();
        //总记录数
        long total = pageTeacher.getTotal();
        //当前页
        long current = pageTeacher.getCurrent();
        //每页记录数
        long size = pageTeacher.getSize();
        //总页数
        long pages = pageTeacher.getPages();
        //是否有上一页
        boolean hasPrevious = pageTeacher.hasPrevious();
        //是否有下一页
        boolean hasNext = pageTeacher.hasNext();
        List<EduTeacher> teacherList = pageTeacher.getRecords();
        map.put("total",total);
        map.put("list",teacherList);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }
}
